﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DeVetma.admin.classes;
using DeVetma.admin.models.database.metadata;
using DeVetma.admin.models.database;
using DeVetma.admin.models.entities;

namespace DeVetma.admin.models.managers
{
    public abstract class DataManager<T> where T:class,new()
    {
        private Database db = Database.instance();

        public virtual bool add(T entity)
        {
            ensureKey(entity);
            return db.addEntity(entity);
        }

        public virtual bool addAll(T[] entities, List<T> failed)
        {
            bool hasFailure = false;

            foreach (var entity in entities)
            {
                ensureKey(entity);
                if (!db.addEntity(entity))
                {
                    hasFailure = true;
                    if (failed != null)
                        failed.Add(entity);
                }
            }

            return !hasFailure;
        }

        public virtual T get(object key)
        {
            return db.getEntity<T>(key);
        }

        public virtual T get(Dictionary<string, object> compositeKey)
        {
            return db.getEntity<T>(compositeKey);
        }

        public virtual List<T> getAll()
        {
            return db.getEntities<T>();
        }

        public virtual bool update(T entity)
        {
            return db.updateEntity(entity);
        }

        public virtual bool updateAll(T[] entities, List<T> failed)
        {
            bool hasFailure = false;

            foreach (var entity in entities)
            {
                if (!db.updateEntity(entity))
                {
                    hasFailure = true;
                    if (failed != null)
                        failed.Add(entity);
                }
            }

            return !hasFailure;
        }

        public virtual bool has(T entity)
        {
            return db.hasEntity<T>(entity);
        }

        public virtual bool delete(T entity)
        {
            return db.deleteEntity(entity);
        }

        public virtual bool deleteAll(T[] entities, List<T> failed)
        {
            bool hasFailure = false;

            foreach (var entity in entities)
            {
                if (!db.deleteEntity(entity))
                {
                    hasFailure = true;
                    if (failed != null)
                        failed.Add(entity);
                }
            }

            return !hasFailure;
        }

        public abstract string ensureKey(T entity);
    }

    public enum ComparisonOperatorType
    {
        Equals,
        NotEquals,
        MoreThan,
        MoreThanEquals,
        LessThan,
        LessThanEquals,
        Between,
        NotBetween,
        BetweenExclusive,
        AndNotEquals,
        OrEquals,
        OrNotEquals
    }
}